﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Checking arguments of a method</title>
  <keywords>
    <keyword translate="true">Resharper</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Checking arguments of a method</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">If you are not using the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="Argument" styleclass="Normal" translate="true">Argument</link><text styleclass="Normal" translate="true"> class, you are definitely missing something! It allows you to check for a method input and make sure it is valid. So, instead of writing this:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">DoSomething(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">myInput)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">    </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">if</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">.IsNullOrWhitespace(myInput)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">    {</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">        Log.Error(&quot;Argument &apos;myInput&apos; cannot be null or whitespace&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">        </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">throw</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ArgumentException(&quot;Argument &apos;myInput&apos; cannot be null or whitespace&quot;,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&quot;myInput&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">    }</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">    // custom logic</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">You can write this:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">DoSomething(</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">myInput)</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">    Argument.IsNotNullOrWhitespace(&quot;myInput&quot;, myInput);</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">    // custom logic</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">However, when you are writing lots of code, then even this piece of code can be too much. Thanks to the Catel.Resharper plugin, it is possible to select the argument (in this case </text><text styleclass="Normal" style="font-style:italic;" translate="true">myInput</text><text styleclass="Normal" style="font-style:normal;" translate="true">), hit ALT + Enter and generate the code, just like the video below:</text></para>
    <para styleclass="Normal"><video src="http://www.youtube.com/v/DLGCLav7ZpU" width="640px" height="390px" autostart="true" loop="false" showcontrols="false" flashversion="6" previewimage scale="100.00%" styleclass="Normal" style="background-color:#ffff00;"></video></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Below is a table what argument checks are available per type:</text></para>
    <para styleclass="Normal"><table styleclass="Default" rowcount="6" colcount="4">
      <tr style="vertical-align:top">
        <td style="width:189px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" style="font-weight:bold;" translate="true">.NET Type</text></para>
        </td>
        <td style="width:209px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" style="font-weight:bold;" translate="true">Available checks</text></para>
        </td>
        <td style="width:206px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" style="font-weight:bold;" translate="true">Exception type</text></para>
        </td>
        <td style="width:596px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" style="font-weight:bold;" translate="true">Constraint text</text></para>
        </td>
      </tr>
      <tr style="vertical-align:top">
        <td style="width:189px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Any reference type</text></para>
        </td>
        <td style="width:209px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsNotNull</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ImplementsInterface</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsOfType</text></para>
        </td>
        <td style="width:206px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentNullException</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentException</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentException</text></para>
        </td>
        <td style="width:596px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is &lt;c&gt;null&lt;/c&gt;</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; does not implement the &lt;see cref=”[interface]” /&gt; interface</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is not of type &lt;see cref=”[type]” /&gt;</text></para>
        </td>
      </tr>
      <tr style="vertical-align:top">
        <td style="width:189px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Array</text></para>
        </td>
        <td style="width:209px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsNotNullOrEmptyArray</text></para>
        </td>
        <td style="width:206px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentException</text></para>
        </td>
        <td style="width:596px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is &lt;c&gt;null&lt;/c&gt; or an empty array</text></para>
        </td>
      </tr>
      <tr style="vertical-align:top">
        <td style="width:189px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Int / decimal / double / float</text></para>
        </td>
        <td style="width:209px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsNotOutOfRange</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsMinimal</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsMaximal</text></para>
        </td>
        <td style="width:206px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentOutOfRangeException</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentOutOfRangeException</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentOutOfRangeException</text></para>
        </td>
        <td style="width:596px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is not between &lt;c&gt;[min]&lt;/c&gt; and &lt;c&gt;[max]&lt;/c&gt;</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is smaller than &lt;c&gt;[value]&lt;/c&gt;</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is larger than &lt;c&gt;[value]&lt;/c&gt;</text></para>
        </td>
      </tr>
      <tr style="vertical-align:top">
        <td style="width:189px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">String</text></para>
        </td>
        <td style="width:209px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsNotNullOrEmpty</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsNotNullOrWhitespace</text></para>
        </td>
        <td style="width:206px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentException</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentException</text></para>
        </td>
        <td style="width:596px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is &lt;c&gt;null&lt;/c&gt; or empty</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is &lt;c&gt;null&lt;/c&gt; or whitespace</text></para>
        </td>
      </tr>
      <tr style="vertical-align:top">
        <td style="width:189px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">Type</text></para>
        </td>
        <td style="width:209px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ImplementsInterface</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">IsOfType</text></para>
        </td>
        <td style="width:206px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentException</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">ArgumentException</text></para>
        </td>
        <td style="width:596px;">
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; does not implement the &lt;see cref=”[interface]” /&gt; interface</text></para>
          <para styleclass="Normal (list)"><text styleclass="Normal (list)" translate="true">The &lt;paramref name=&quot;[paramname]&quot;/&gt; is not of type &lt;see cref=”[type]” /&gt;</text></para>
        </td>
      </tr>
    </table></para>
  </body>
</topic>
